package com.jyw.service;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.jyw.activity.CouponActivity;
import com.jyw.activity.OrderPayActivity;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.pojo.EventBusTypes;

import org.json.JSONException;

import java.io.IOException;

import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/6
 */

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */

public class GetIntentService extends GTIntentService {

    @Override
    public void onReceiveServicePid(Context context, int pid) {
        Log.e("simonwLog", "onReceiveServicePId -> " + "pid = " + pid);
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        /*Intent intent =new Intent(context, CzActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);*/
        Log.e("simonwLog", "onReceiveMessageData -> " + "MessageData = " + new String(msg.getPayload()));
        goThroughCouponOrNot(context, new String(msg.getPayload()));
    }

    private void goThroughCouponOrNot(final Context ctx, final String id) {
        Request request = new Request.Builder().
                url(Server.GET_COUPON + "?phone_data=" + getApplication().getSharedPreferences("jyw", Context.MODE_PRIVATE).getString("phone", "")).
                build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(ctx, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    org.json.JSONArray arr = new org.json.JSONArray(response.body().string());
                    if (arr.length() == 0) {
                        Intent intent = new Intent(ctx, OrderPayActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("order_id", id);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(ctx, CouponActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("order_id", id);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e("simonwLog", "onReceiveClientId -> " + "clientid = " + clientid);
        EventBus.getDefault().post(new EventBusTypes.ObtainClientId(clientid));
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
        Log.e("simonwLog", "onReceiveOnlineState -> " + "online = " + online);
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
        Log.e("simonwLog", "onReceiveCommandResult -> " + "cmdMessage = " + cmdMessage);
    }
}
